package com.gitee.wsl.compose.ui.picker.icon

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.CreationExtras
import com.gitee.wsl.compose.ui.picker.icon.data.EmojiProvider
import com.gitee.wsl.compose.ui.picker.icon.suggest.EmojiSuggester

import kotlinx.coroutines.flow.MutableStateFlow
import kotlin.reflect.KClass

class TypeIconPickerViewModel(
    provider: EmojiProvider,
    suggester: EmojiSuggester,
) : IconPickerViewModel<Unit>(provider, suggester) {

    val actions = MutableStateFlow<EmojiPickerAction>(EmojiPickerAction.Idle)

    override fun setEmoji(iconable: Unit, emojiUnicode: String) {
        actions.value = EmojiPickerAction.SetEmoji(emojiUnicode)
    }

    override fun onRemoveClicked(iconable: Unit) {
        actions.value = EmojiPickerAction.RemoveEmoji
    }

    //override fun onPickedFromDevice(iconable: Unit, path: String, space: Id) {
        // Do nothing
    //}

    sealed class EmojiPickerAction {
        data object Idle : EmojiPickerAction()
        class SetEmoji(val emojiUnicode: String) : EmojiPickerAction()
        data object RemoveEmoji : EmojiPickerAction()
    }

    class Factory(
        private val emojiSuggester: EmojiSuggester,
        private val emojiProvider: EmojiProvider
    ) : ViewModelProvider.Factory {

        @Suppress("UNCHECKED_CAST")
        override fun <T : ViewModel> create(modelClass: KClass<T>,
                                            extras: CreationExtras): T {
            return TypeIconPickerViewModel(
                suggester = emojiSuggester,
                provider = emojiProvider
            ) as T
        }
    }

}
